package jdbc.preparestatement_;

import java.io.FileInputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.Properties;
import java.util.Scanner;

/**
 * @author DCSGO
 * @version 1.0
 * PrepareStatement 增删改
 */
@SuppressWarnings({"all"})
public class PrepareStatementDML {
    public static void main(String[] args) throws Exception {
        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入管理员名：");
        String admin_name = scanner.nextLine();// 读取空格
        System.out.println("请输入密码：");
        String admin_pwd = scanner.nextLine();
        Properties properties = new Properties();
        properties.load(new FileInputStream("src\\mysql.properties"));
        String url = properties.getProperty("url");
        String user = properties.getProperty("user");
        String password = properties.getProperty("password");
        String Driver = properties.getProperty("Driver");
        //1.注册驱动
        Class.forName(Driver);
        //2.获取连接
        Connection connection = DriverManager.getConnection(url, user, password);
        //3.获取 PrepareStatement
        //String sql = "insert into admin values (?,?)";//? 为占位符
        //String sql = "update admin set pwd = ? where `name` = ?";//? 为占位符
        String sql = "delete from admin where `name` = ? and pwd = ?";//? 为占位符
        PreparedStatement prepareStatement = connection.prepareStatement(sql);//在获取时需要填写一个 sql 语句
        //4.执行 dml 语句
        prepareStatement.setString(1, admin_name);//设置sql语句中第几个?的值为啥
        prepareStatement.setString(2, admin_pwd);
        int rows = prepareStatement.executeUpdate();
        System.out.println(rows > 0 ? "执行成功" : "执行失败");

        prepareStatement.close();
        connection.close();
    }
}
